package com.weiyan.blog.zuulfilter;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.weiyan.blog.utils.EncryptUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;

import java.util.*;

/**
 * @author misterWei
 * @create 2019年06月22号:13点18分
 * @mailbox mynameisweiyan@gmail.com
 */

/**
 * 网关过滤器配置
 */

public class WebZuulFilter extends com.netflix.zuul.ZuulFilter {
    /**
     * pre 代表执行前去经过过滤器
     * post 代表执行后去经过过滤器
     * @return
     */
    @Override
    public String filterType() {

        return "pre";
    }

    /**
     * 假设有多个过滤器,配置n多个 int就是用来进行排序的 0代表第一个执行
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * true  开启此过滤器
     * false 关闭此过滤器
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤那些内容 返回null代表执行
     *  setSendZuulResponse(false); 代表不执行
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
      //认证解析oauth2 令牌 方式
        RequestContext cxf = RequestContext.getCurrentContext();
        //获取已经解析过的令牌机制

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
         if (!(authentication instanceof OAuth2Authentication)){
             return null;
         }
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
        //拿到用户信息
        Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
        if (Objects.isNull(userAuthentication)) {
             return null;
        }
        String principal = userAuthentication.getName();

        //拿到权限
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            List<String> authoritieList = new ArrayList<>();
        Collection<? extends GrantedAuthority> authorities = userAuthentication.getAuthorities();
        authorities.stream().forEach(a->authoritieList.add(a.getAuthority()));

        //拿到其它的请求信息
        OAuth2Request oAuth2Request = oAuth2Authentication.getOAuth2Request();
        Map<String, String> requestParameters = oAuth2Request.getRequestParameters();
        Map<String,Object> jsonMap = new HashMap<>(requestParameters);
        if (userAuthentication != null){
            //装载到map中
            jsonMap.put("principal",principal);
            jsonMap.put("authorities",authoritieList);
        }
        //载入请求头信息
        cxf.addZuulRequestHeader("token", EncryptUtil.encryptRAM( JSON.toJSONString(jsonMap)));
        return null;
    }
}
